package org.example;

public class solution1 {
    public static int maxConsecutiveTarget(int target, int[] bits) {
        int left = 0;
        int flipCount = 0;
        int maxLen = 0;

        for (int right = 0; right < bits.length; right++) {
            if (bits[right] != target) {
                flipCount++;
            }

            while (flipCount > 1) {
                if (bits[left] != target) {
                    flipCount--;
                }
                left++;
            }

            maxLen = Math.max(maxLen, right - left + 1);
        }

        return maxLen;
    }

//    // 测试
//    public static void main(String[] args) {
////        int target = 1;
////        int[] bits = {0, 1, 1, 0, 1, 0, 1, 0, 0};
//
//        int target = 0;
//        int[] bits = {0, 1, 1, 0, 1, 0, 1, 0, 0};
//
//        int result = maxConsecutiveTarget(target, bits);
//        System.out.println("最大连续 " + target + " 的个数是: " + result);  // 输出: 4
//    }
}
